import os,csv,random
import numpy as np
pic_path = r'/home/ubuntu/data_tongue'
feature_path = r'/home/ubuntu/data/main_label_feature.csv'
feature_list = csv.reader(open(feature_path,'r'))
feature_dict = {}
nums = 6
# file_label = open('/home/ubuntu/data/multi_label/data/train_test_5.txt','r').readlines()
# for file in file_label:
#     key = file.strip('\n').split('\t')[0][8:]
#     # feature_dict[key] = [str(round(float(file[2:][i]))) for i in range(15)]
#     feature_dict[key] = float(file.strip('\n').split('\t')[-1])
for file in feature_list:
    key = file[1].split('!')[0]
    # feature_dict[key] = [str(round(float(file[2:][i]))) for i in range(15)]
    feature_dict[key] = float(file[2:][nums-1])

s =0
t = 0
numx = 0
numy = 0
numz = 0
numxt = 0
numyt = 0
# f = open('/home/ubuntu/data/multi_label/data/data_all_option_count.txt','r').readlines()
# for i in f:
#     i.strip('\n').split('')

# for files in os.listdir('/home/ubuntu/data/inquiry_tongue'):
#     key = files[:-4].split('!')[0][8:]
#
#     if key in feature_dict.keys() and feature_dict[key][0] != '-1':
#         s += 1
#         print(s)
#         f.writelines(files+','+','.join(feature_dict[key])+'\n')
for i in os.listdir(pic_path):

    if  i.startswith('source') or i.endswith('_top'):
        if i.startswith('source'):
            i = i + r'/top'
        list_pics = os.listdir(os.path.join(pic_path, i))
        random.shuffle(list_pics)
        print(os.path.join(pic_path, i))
        for count,file in enumerate(list_pics):

            file_source = file
            file = file.split('!')[0]
            ############ single
            # if file in feature_dict.keys() and feature_dict[file]>-1:
                # path = r'/home/ubuntu/data/Image_Retrieval/dataset/feature_test/0'
                # if not os.path.exists(os.path.join(path, file)):
                #     os.symlink(os.path.join(pic_path, i, file), os.path.join(path,  file))
            if file in feature_dict.keys() :


                s += 1
                if count % 10 == 0:
                    path = r'/home/ubuntu/data/dataset/pr'+str(nums)+'_test'
                    if not os.path.exists(path):
                        os.mkdir(path)

                    if feature_dict[file] == 1. or feature_dict[file] == 2 or feature_dict[file] == 0. \
                              :
                        if feature_dict[file] == 0.:
                            numxt += 1
                            if numxt > 2000:
                                continue
                        elif feature_dict[file] ==1.:
                            numyt += 1
                            if numyt > 2000:
                                continue
                        if not os.path.exists(os.path.join(path, str(int(feature_dict[file])), file_source)):
                            if os.path.exists(os.path.join(pic_path, i, file_source)):

                                if not os.path.exists(os.path.join(path, str(int(feature_dict[file])))):
                                    os.mkdir(os.path.join(path, str(int(feature_dict[file]))))
                                os.symlink(os.path.join(pic_path, i, file_source),
                                           os.path.join(path, str(int(feature_dict[file])), file_source))
                else:

                    path = r'/home/ubuntu/data/dataset/pr' + str(nums) + '_train'
                    if not os.path.exists(path):
                        os.mkdir(path)
                    if feature_dict[file] >1.5 :
                        feature_dict[file] = 2.
                    if feature_dict[file] <0.5 :
                        feature_dict[file] = 0.
                        numx += 1
                        if numx >30000:
                            continue
                    elif feature_dict[file] <1.5 :
                        feature_dict[file] = 1.
                        numy += 1
                        if numy >30000:
                            continue
                    if not os.path.exists(os.path.join(path,str(round(feature_dict[file])),file_source)) :
                        if os.path.exists(os.path.join(pic_path,i,file_source)):

                            if not os.path.exists(os.path.join(path, str(round(feature_dict[file])))):
                                os.mkdir(os.path.join(path, str(round(feature_dict[file]))))
                            os.symlink(os.path.join(pic_path,i,file_source),os.path.join(path,str(round(feature_dict[file])),file_source))

        print(s,numx,numy,numxt,numyt)

        ##########    muti
        # if file in feature_dict.keys():
        #     feature = feature_dict[file].split(',')
        #     feature1 = float(feature[0])
        #     feature2 = float(feature[1])
        #     if feature1>-1 and feature2>-1:
        #
        #         if count % 8 == 0:
        #             path = r'/home/ubuntu/data/dataset/test'
        #             # else:
        #             #     path = r'/home/ubuntu/data/dataset/train'
        #             file_list = [str(i)+str(j) for i in range(3) for j in range(4)]
        #             if feature[0] + feature[1] in file_list :
        #                 if not os.path.exists(os.path.join(path, feature[0] + feature[1])):
        #                     os.mkdir(os.path.join(path, feature[0] + feature[1]))
        #                 if not os.path.exists(os.path.join(path, feature[0] + feature[1], file)):
        #                     if os.path.exists(os.path.join(pic_path, i, file)):
        #                         # print(os.path.join(pic_path,i,file))
        #                         s += 1
        #                         print(s)
        #
        #                         os.symlink(os.path.join(pic_path, i, file),
        #                                    os.path.join(path, feature[0]+feature[1], file))
        #
        #         else:
        #
        #             path = r'/home/ubuntu/data/dataset/train'
        #             if feature1 > 2:
        #                 feature1 = 2
        #             if feature2 > 3:
        #                 feature2 = 3
        #             if not os.path.exists(os.path.join(path, str(round(feature1)) + str(round(feature2)))):
        #                 os.mkdir(os.path.join(path, str(round(feature1)) + str(round(feature2))))
        #             if not os.path.exists(os.path.join(path, str(round(feature1)) + str(round(feature2)), file)):
        #                 if os.path.exists(os.path.join(pic_path, i, file)):
        #                     # print(os.path.join(pic_path,i,file))
        #                     s += 1
        #                     print(s)
        #
        #                     os.symlink(os.path.join(pic_path, i, file),
        #                                os.path.join(path, str(round(feature1)) + str(round(feature2)), file))
        #


############ label by people

#             if feature_dict[file] == 0:
#                 f0 +=1
#             elif feature_dict[file] == 0.33:
#                 f1 += 1
#             elif feature_dict[file] == 0.67:
#                 f2 += 1
#             elif feature_dict[file] == 1:
#                 f3 += 1
#             elif feature_dict[file] == 1.33:
#                 f4 += 1
#             elif feature_dict[file] == 1.67:
#                 f5 += 1
#             elif feature_dict[file] == 2:
#                 f6 += 1
#             elif feature_dict[file] == 2.33:
#                 f7 += 1
#             elif feature_dict[file] == 2.67:
#                 f8 += 1
#             elif feature_dict[file] == 3:
#                 f9 += 1
#             elif feature_dict[file] == 3.33:
#                 f10 += 1
#             elif feature_dict[file] == 3.67:
#                 f11 += 1
#             elif feature_dict[file] == 4:
#                 f12 += 1
#             elif feature_dict[file] == 4.33:
#                 f13 += 1
#             elif feature_dict[file] == 4.67:
#                 f14 += 1
#             elif feature_dict[file] == 5:
#                 f15 += 1
#             elif feature_dict[file] == 5.33:
#                 f16 += 1
#             elif feature_dict[file] == 5.67:
#                 f17 += 1
#             elif feature_dict[file] == 6:
#                 f18 += 1
#             elif feature_dict[file] == 6.33:
#                 f19 += 1
#             elif feature_dict[file] == 6.67:
#                 f20 += 1
#             elif feature_dict[file] == 7:
#                 f21 += 1
#             elif feature_dict[file] == 7.33:
#                 f22 += 1
#             elif feature_dict[file] > 7.33:
#                 f23 += 1
# print(f0,f1,f2,f3,f4,f5,f6,f7,f8)
# q0 = (f0+2*f1/3)/(f0+f1)
# q1 = (f3+2*f2/3+2*f4/3)/(f3+f2+f4)
# q2 = (f6+2*f5/3+2*f7/3)/(f6+f5+f7)
# q3 = (f9+2*f8/3+2*f10/3)/(f9+f8+f10)
# q4 = (f12+2*f11/3+2*f13/3)/(f12+f11+f13)
# q5 = (f15+2*f14/3+2*f16/3)/(f15+f14+f16)
# q6 = (f18+2*f17/3+2*f19/3)/(f18+f17+f19)
# q7 = (f21+2*f20/3+2*f22/3)/(f21+f20+f22+f23)
# print('0:{:.2f}'.format(q0))
# print('1:{:.2f}'.format(q1))
# print('2:{:.2f}'.format(q2))
# print('3:{:.2f}'.format(q3))
# print('4:{:.2f}'.format(q4))
# print('5:{:.2f}'.format(q5))
# print('6:{:.2f}'.format(q6))
# print('7:{:.2f}'.format(q7))
# print('mean:{:.2f}'.format((q0+q1+q2+q3+q4+q5+q6+q7)/8))


# import os,csv,shutil
# pic_list = ['source_0108','source_1102','source_1103','source_1119','source_1205','source_1210','source_1211']
# pic_path = r'D:\图片筛选'
# feature_path = r'D:\tongue\main_label_feature.csv'
# feature_list = csv.reader(open(feature_path,'r'))
# feature_dict = {}
# for file in feature_list:
#     feature_dict[file[1]] = round(float(file[2:][5]))
# s =0
# for i in pic_list:
#     for file in os.listdir(os.path.join(pic_path,i,'top')):
#         if file in feature_dict.keys() and feature_dict[file] >1:
#             print(i)
#             shutil.copy(os.path.join(pic_path,i,'top',file),os.path.join(r'D:\图片筛选\feature_6_class2',file))
#

# import os,csv,shutil
# f = open('/home/ubuntu/data/multi_label/test/inquiry/example_2000.txt','r')
# pic_path = r'/home/ubuntu/data_tongue'
# dcit = {}
# for file in os.listdir('/home/ubuntu/data_tongue'):
#     if file.endswith('top') :
#         path = '/home/ubuntu/data_tongue/'+file
#         for fi in os.listdir(path):
#             fikey = fi[8:].split('!')[0]
#             dcit[fikey] = os.path.join(path,fi)
#     if  file.startswith('source'):
#         path = os.path.join('/home/ubuntu/data_tongue/' + file, 'top')
#         for fi in os.listdir(path):
#             fikey = fi[8:].split('!')[0]
#             dcit[fikey] = os.path.join(path,fi)
#
# num = 1000
# fil = open('/home/ubuntu/data/multi_label/test/inquiry/exa.txt','r')
# dis = {'Wet':'1','Normal':'0'}
# file = open('/home/ubuntu/data/multi_label/test/inquiry/gsg.txt','r')
# predict = []
# target = []
# index = []
# for num,fi in enumerate(file.readlines()):
#     if num < 200:
#         if fi.strip('\n') == '':
#             index.append(num)
#         else:
#             predict.append(fi.strip('\n'))
#
# for num,file in enumerate(fil.readlines()):
#     if num<200:
#         if num not in index:
#             target.append(file.split(' ')[-1].strip('\n'))
# from sklearn import metrics
#
# precision_score = metrics.precision_score(target, predict,labels=['1'], average='macro')
# recall_score = metrics.recall_score(target, predict, labels=['1'],average='macro')
# print('precision:',precision_score)
# print('recall:', recall_score)
# for file in f.readlines():
#     key = file.split('\t')[0].split('-')[-1]
#     value = file.split('\t')[-1].strip('\n')
#     if  key in dcit.keys():
#         num += 1
#         shutil.copy(dcit[key],'/home/ubuntu/data/dataset/pr7_test/pics_wet_if/'+str(num)+'.jpg')
#         fil.write(str(num)+'.jpg'+' '+ dis[value]+'\n')